﻿// Nail.ImageConverter Library
// Nail framework
//
// Copyright © Frank Nagl, 2008
// admin@franknagl.de
// www.franknagl.de
//
namespace Nail
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    /// <summary>
    /// Provides several static functions to convert one or multiple image file(s) in a 
    /// special image format.
    /// </summary>
    /// <remarks>
    /// <para>Supported formats:</para>
    /// <list type="enumeration">BMP</list>
    /// <list type="enumeration">EMF</list>
    /// <list type="enumeration">GIF</list>
    /// <list type="enumeration">JPG</list>
    /// <list type="enumeration">PNG</list>
    /// <list type="enumeration">TIFF</list>
    /// <list type="enumeration">WMF</list>
    /// </remarks>
    public static class ImageConverter
    {
        #region General Converting
        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, format, 80, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="jpegQuality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string imageFile, 
            bool overwrite, byte jpegQuality)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, format, jpegQuality, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <param name="jpegQuality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string imageFile,
            bool overwrite, byte jpegQuality, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, format, jpegQuality, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="jpegQuality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string imageFile, bool overwrite,
            byte jpegQuality, int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height, format, jpegQuality,
                correctForge);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="deleteOriginal">True if the original image should be deleted
        /// after converting, otherwise false.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string originalFile, 
            string newFile, bool deleteOriginal)
        {
            return Core(originalFile, newFile, deleteOriginal, 0, 0, format, 80, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="jpegQuality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <param name="deleteOriginal">True if the original image should be deleted
        /// after converting, otherwise false.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string originalFile,
            string newFile, byte jpegQuality, bool deleteOriginal)
        {
            return Core(originalFile, newFile, deleteOriginal, 0, 0, format, jpegQuality, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="jpegQuality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="deleteOriginal">True if the original image should be deleted
        /// after converting, otherwise false.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string originalFile,
            string newFile, byte jpegQuality, int biggerSideLength, bool deleteOriginal)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, deleteOriginal, (int)w, (int)h, format, jpegQuality, false);
        }

        /// <summary>
        /// Converts an image file to the passed encoding format.
        /// </summary>
        /// <param name="format">The encoding format.</param>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="quality">The JPEG quality (if JPEG encoder is choosen).
        /// Value has to be between 0 and 100.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <param name="deleteOriginal">True if the original image should be deleted
        /// after converting, otherwise false.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ConvertTo(ImageFormat format, string originalFile, string newFile,
            byte quality, int width, int height, bool correctForge, bool deleteOriginal)
        {
            return Core(originalFile, newFile, deleteOriginal, width, height,
                format, quality, correctForge);
        }
        #endregion General Converting

        #region JPG
        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Jpeg, 80, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string imageFile, bool overwrite, byte quality)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Jpeg, quality, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string imageFile, bool overwrite, byte quality, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Jpeg, quality, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string imageFile, bool overwrite, byte quality, 
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height, ImageFormat.Jpeg, quality, 
                correctForge);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Jpeg, 80, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string originalFile, string newFile, byte quality)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Jpeg, quality, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string originalFile, string newFile, byte quality, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Jpeg, quality, false);
        }

        /// <summary>
        /// Converts an image file to a JPG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToJPG(string originalFile, string newFile, byte quality,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, 
                ImageFormat.Jpeg, quality, correctForge);
        }
        #endregion JPG

        #region JPG list
        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if(!Core(pic, pic, overwrite, 0, 0, ImageFormat.Jpeg, 80, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to JPGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] imageFiles, bool overwrite, byte quality)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Jpeg, quality, false))
                return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to JPGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] imageFiles, bool overwrite, byte quality, 
            int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Jpeg, quality, false))
                    return false;                
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] imageFiles, bool overwrite, byte quality, 
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Jpeg, quality, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for(int i = 0; i < originalFiles.Length; i++)
                if(!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Jpeg, 80, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] originalFiles, string[] newFiles, byte quality)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for(int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Jpeg, quality, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] originalFiles, string[] newFiles, byte quality, 
            int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }            
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if(!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Jpeg, quality, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to JPGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="quality">The quality. Value has to be between 0 and 100.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToJPGs(string[] originalFiles, string[] newFiles, byte quality, 
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if(!Core(originalFiles[i], newFiles[i], false, width, height, 
                    ImageFormat.Jpeg, quality, correctForge))
                    return false;
            return true;
        }
        #endregion JPG list

        #region PNG
        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToPNG(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Png, 0, false);
        }

        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToPNG(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Png, 0, false);
        }

        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToPNG(string imageFile, bool overwrite, 
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height, 
                ImageFormat.Png, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToPNG(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Png, 0, false);
        }

        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToPNG(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Png, 0, false);
        }

        /// <summary>
        /// Converts an image file to a PNG.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToPNG(string originalFile, string newFile, 
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Png, 0, correctForge);
        }
        #endregion PNG

        #region PNG list
        /// <summary>
        /// Converts multiple image files to PNGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Png, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to PNGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Png, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to PNGs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] imageFiles, bool overwrite, 
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Png, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to PNGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Png, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to PNGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h, 
                    ImageFormat.Png, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to PNGs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToPNGs(string[] originalFiles, string[] newFiles, 
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height, 
                    ImageFormat.Png, 0, correctForge))
                    return false;
            return true;
        }
        #endregion PNG list

        #region BMP
        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToBMP(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Bmp, 0, false);
        }

        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToBMP(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Bmp, 0, false);
        }

        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToBMP(string imageFile, bool overwrite,
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height,
                ImageFormat.Bmp, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToBMP(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Bmp, 0, false);
        }

        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToBMP(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Bmp, 0, false);
        }

        /// <summary>
        /// Converts an image file to a BMP.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToBMP(string originalFile, string newFile,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Bmp, 0, correctForge);
        }
        #endregion BMP

        #region BMP list
        /// <summary>
        /// Converts multiple image files to BMPs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Bmp, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to BMPs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Bmp, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to BMPs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] imageFiles, bool overwrite,
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Bmp, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to BMPs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Bmp, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to BMPs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Bmp, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to BMPs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToBMPs(string[] originalFiles, string[] newFiles,
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height,
                    ImageFormat.Bmp, 0, correctForge))
                    return false;
            return true;
        }
        #endregion BMP list

        #region GIF
        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToGIF(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Gif, 0, false);
        }

        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToGIF(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Gif, 0, false);
        }

        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToGIF(string imageFile, bool overwrite,
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height,
                ImageFormat.Gif, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToGIF(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Gif, 0, false);
        }

        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToGIF(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Gif, 0, false);
        }

        /// <summary>
        /// Converts an image file to a GIF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToGIF(string originalFile, string newFile,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Gif, 0, correctForge);
        }
        #endregion GIF

        #region GIF list
        /// <summary>
        /// Converts multiple image files to GIFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Gif, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to GIFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Gif, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to GIFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] imageFiles, bool overwrite,
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Gif, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to GIFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Gif, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to GIFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Gif, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to GIFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToGIFs(string[] originalFiles, string[] newFiles,
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height,
                    ImageFormat.Gif, 0, correctForge))
                    return false;
            return true;
        }
        #endregion GIF list

        #region EMF
        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToEMF(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Emf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToEMF(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Emf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, 
        /// otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToEMF(string imageFile, bool overwrite,
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height,
                ImageFormat.Emf, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToEMF(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Emf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToEMF(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Emf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a EMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, 
        /// otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToEMF(string originalFile, string newFile,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Emf, 0, correctForge);
        }
        #endregion EMF

        #region EMF list
        /// <summary>
        /// Converts multiple image files to EMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Emf, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to EMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Emf, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to EMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] imageFiles, bool overwrite,
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Emf, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to EMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Emf, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to EMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Emf, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to EMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToEMFs(string[] originalFiles, string[] newFiles,
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height,
                    ImageFormat.Emf, 0, correctForge))
                    return false;
            return true;
        }
        #endregion EMF list

        #region TIFF
        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToTIFF(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Tiff, 0, false);
        }

        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToTIFF(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Tiff, 0, false);
        }

        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToTIFF(string imageFile, bool overwrite,
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height,
                ImageFormat.Tiff, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToTIFF(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Tiff, 0, false);
        }

        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToTIFF(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Tiff, 0, false);
        }

        /// <summary>
        /// Converts an image file to a TIFF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToTIFF(string originalFile, string newFile,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Tiff, 0, correctForge);
        }
        #endregion TIFF

        #region TIFF list
        /// <summary>
        /// Converts multiple image files to TIFFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Tiff, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to TIFFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Tiff, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to TIFFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] imageFiles, bool overwrite,
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Tiff, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to TIFFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Tiff, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to TIFFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Tiff, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to TIFFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToTIFFs(string[] originalFiles, string[] newFiles,
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height,
                    ImageFormat.Tiff, 0, correctForge))
                    return false;
            return true;
        }
        #endregion TIFF list

        #region WMF
        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>True if the converting process was successful, otherwise false</returns>
        public static bool ToWMF(string imageFile, bool overwrite)
        {
            return Core(imageFile, imageFile, overwrite, 0, 0, ImageFormat.Wmf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="overwrite">If <c>true</c> the 
        /// original file will be overwritten or deleted.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToWMF(string imageFile, bool overwrite, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(imageFile, biggerSideLength, out w, out h);
            return Core(imageFile, imageFile, overwrite, (int)w, (int)h, ImageFormat.Wmf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToWMF(string imageFile, bool overwrite,
            int width, int height, bool correctForge)
        {
            return Core(imageFile, imageFile, overwrite, width, height,
                ImageFormat.Wmf, 0, correctForge);
        }

        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false
        /// </returns>
        public static bool ToWMF(string originalFile, string newFile)
        {
            return Core(originalFile, newFile, false, 0, 0, ImageFormat.Wmf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToWMF(string originalFile, string newFile, int biggerSideLength)
        {
            float w, h;
            CalcBiggerSideLength(originalFile, biggerSideLength, out w, out h);
            return Core(originalFile, newFile, false, (int)w, (int)h, ImageFormat.Wmf, 0, false);
        }

        /// <summary>
        /// Converts an image file to a WMF.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if the converting process was successful, otherwise false.
        /// </returns>
        public static bool ToWMF(string originalFile, string newFile,
            int width, int height, bool correctForge)
        {
            return Core(originalFile, newFile, false, width, height, ImageFormat.Wmf, 0, correctForge);
        }
        #endregion WMF

        #region WMF list
        /// <summary>
        /// Converts multiple image files to WMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] imageFiles, bool overwrite)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, 0, 0, ImageFormat.Wmf, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image file to WMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] imageFiles, bool overwrite, int biggerSideLength)
        {
            float w, h;
            foreach (string pic in imageFiles)
            {
                CalcBiggerSideLength(pic, biggerSideLength, out w, out h);
                if (!Core(pic, pic, overwrite, (int)w, (int)h, ImageFormat.Wmf, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to WMFs.
        /// </summary>
        /// <param name="imageFiles">The image files.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original files will be overwritten or deleted.</param>
        /// <param name="width">The width of the images.</param>
        /// <param name="height">The height of the images.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] imageFiles, bool overwrite,
            int width, int height, bool correctForge)
        {
            foreach (string pic in imageFiles)
                if (!Core(pic, pic, overwrite, width, height, ImageFormat.Wmf, 0, correctForge))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to WMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] originalFiles, string[] newFiles)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, 0, 0, ImageFormat.Wmf, 0, false))
                    return false;
            return true;
        }

        /// <summary>
        /// Converts multiple image files to WMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="biggerSideLength">Length of the bigger side of an image.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] originalFiles, string[] newFiles, int biggerSideLength)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException("newFiles must have same length like originalFiles.");
                //return false;
            }
            float w, h;
            for (int i = 0; i < originalFiles.Length; i++)
            {
                CalcBiggerSideLength(originalFiles[i], biggerSideLength, out w, out h);
                if (!Core(originalFiles[i], newFiles[i], false, (int)w, (int)h,
                    ImageFormat.Wmf, 0, false))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Converts multiple image files to WMFs.
        /// </summary>
        /// <param name="originalFiles">The original files.</param>
        /// <param name="newFiles">The new files.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio will be hold, otherwise not.</param>
        /// <returns>
        /// True if all converting processes were successful, otherwise false.
        /// </returns>
        public static bool ToWMFs(string[] originalFiles, string[] newFiles,
            int width, int height, bool correctForge)
        {
            if (originalFiles.Length != newFiles.Length)
            {
                throw new ArgumentOutOfRangeException
                    ("newFiles must have same length like originalFiles.");
                //return false;
            }
            for (int i = 0; i < originalFiles.Length; i++)
                if (!Core(originalFiles[i], newFiles[i], false, width, height,
                    ImageFormat.Wmf, 0, correctForge))
                    return false;
            return true;
        }
        #endregion WMF list

        #region Private helper functions
        /// <summary>
        /// Calcs the length of the bigger side.
        /// </summary>
        /// <param name="imageFile">The image file.</param>
        /// <param name="biggerSideLength">Length of the bigger side of the image.</param>
        /// <param name="w">The width.</param>
        /// <param name="h">The height.</param>
        private static void CalcBiggerSideLength(string imageFile, 
                                                 int biggerSideLength,
                                                 out float w,
                                                 out float h)
        {
            w = h = 0;
            try
            {
                Bitmap pic = new Bitmap(imageFile);
                w = (float)pic.Width;
                h = (float)pic.Height;
                float ratio = w / h;
                if (w > h)
                {
                    w = biggerSideLength;
                    h = w / ratio;
                }
                else
                {
                    h = biggerSideLength;
                    w = h * ratio;
                }
                pic.Dispose();
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Transforms an image with the passed variables adn saves it.
        /// </summary>
        /// <param name="originalFile">The original file.</param>
        /// <param name="newFile">The new file.</param>
        /// <param name="overwrite">If <c>true</c> the
        /// original file will be overwritten or deleted.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="format">The format.</param>
        /// <param name="quality">The quality.</param>
        /// <param name="correctForge">If <c>true</c> the aspect ratio 
        /// will be hold, otherwise not.</param>
        /// <returns></returns>
        private static bool Core
            (String originalFile, 
             String newFile, 
             bool overwrite,
             int width, 
             int height, 
             ImageFormat format, 
             byte quality,
             bool correctForge)
        {
            try
            {
                Bitmap pic = new Bitmap(originalFile);                              
                //Store the original EXIF infos
                PropertyItem[] items = pic.PropertyItems;
                
                if (width == 0 || height == 0)
                {
                    width = pic.Width;
                    height = pic.Height;
                }
                float ratio = (float)width / (float)height;
                float h = (float)pic.Height;
                float w = (float)pic.Width;
                float xStart = 0f;
                float yStart = 0f;
                float wCorrect = w;
                float hCorrect = h;

                if (correctForge)
                {
                    if (w / h > ratio)
                    {
                        wCorrect = h * ratio;
                        xStart = (w - wCorrect) / 2.0f;
                    }
                    else if (w / h <= ratio)
                    {
                        hCorrect = w / ratio;
                        yStart = (h - hCorrect) / 2.0f;
                    }
                }

                RectangleF rec = new RectangleF(xStart, yStart, wCorrect, hCorrect);
                Bitmap croppedPic = pic.Clone(rec, pic.PixelFormat);
                pic.Dispose();
                croppedPic = new Bitmap(croppedPic, width, height);
                MemoryReducer.ReduceMemoryUsage();

                #region Correct the newFile name
                newFile = newFile.Substring(0, newFile.LastIndexOf('.') + 1) +
                    format.ToString().Replace("Jpeg", "jpg").ToLower();
                if (newFile.ToLower() != originalFile.ToLower())
                {
                    if (overwrite)
                        File.Delete(originalFile);
                }
                else if (!overwrite)
                {
                    newFile = newFile.Substring(0, newFile.LastIndexOf('.')) +
                    "_1." + format.ToString().Replace("Jpeg", "jpg").ToLower();
                }
                #endregion Correct the newFile name
                
                //Create destination directory, if it doesn't exist already
                newFile = newFile.Replace('\\', '/');
                int index = newFile.LastIndexOf('/');
                if (index != -1 && !Directory.Exists(newFile.Substring(0, index)))
                    Directory.CreateDirectory(newFile.Substring(0, index));

                //Before saving, set the original EXIF infos                
                foreach (PropertyItem item in items)                 
                    croppedPic.SetPropertyItem(item);

                //Saving processes
                if (format == ImageFormat.Jpeg)              
                    JpegEncoder.SaveJpeg(newFile, croppedPic, quality);
                else
                    croppedPic.Save(newFile, format);
                croppedPic.Dispose();                    
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return false;
            }
            return true;
        }
        #endregion Private helper functions
    }
}
